/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sac;

import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

/**
 *
 * @author Vera
 */
public class Main {
    public enum Categories {
        Electronics,
        Books,
        Clothes,
        Music,
        Movies
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("MySQL Connect Example.");
        Connection conn = null;
        String dbtime;
        String url = "jdbc:mysql://127.0.0.1:3306/";
        String dbName = "sac_recomandari";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "admin";
        String query = "Select * FROM categorii";
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url+dbName,userName,password);
            System.out.println("Connected to the database");
            Statement stmt = (Statement) conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);
           /* while (rs.next()) {
                dbtime = rs.getString(2);
                System.out.println(dbtime);
             }*/
             String current = args[0];
             Categories currentCat = Categories.valueOf(current);
             Integer id_cat=0;
             System.out.println(args[2]);
             switch (currentCat)
             {
                 case Electronics:
                    id_cat = 1;
                    break;
                 case Books:
                    id_cat = 4;
                    break;
                 case Clothes:
                     id_cat = 5;
                     break;
                 case Music:
                     id_cat = 6;
                     break;
                 case Movies:
                     id_cat = 7;
                     break;
             }
            
             query = "SELECT DISTINCT c.nume FROM" +
                     "(SELECT DISTINCT atribute_cadouri.id_cadou AS id_cadou," +
                     "atribute_cadouri.valoare AS sex FROM atribute_cadouri, cadouri " +
                     "WHERE atribute_cadouri.id_cadou = cadouri.id " +
                     "AND cadouri.id_cat ="+id_cat+" AND atribute_cadouri.valoare LIKE '"+args[1]+"%')"+
                     "AS a, atribute_cadouri as b, cadouri as C " +
                     "WHERE a.id_cadou = b.id_cadou " +
                     "AND b.valoare LIKE '"+args[2]+"%' AND c.id=a.id_cadou";

            stmt = (Statement) conn.createStatement();
            rs = stmt.executeQuery(query);
            while (rs.next()) {
                dbtime = rs.getString(1);
                //dbtime = rs.getString(1)+"-"+ rs.getString(2)+"-"+ rs.getString(3);
                System.out.println(dbtime);
             }

            conn.close();
            System.out.println("Disconnected from database");
         } catch (Exception e) {
             e.printStackTrace();
           }
      }
    }

